//
//  NetWorkManager.swift
//  Pods
//
//  Created by HongPu on 2015/8/3.
//
//

import Foundation

//
let TIMEOUT: NSTimeInterval = 30
private let NetWorkManagerHandle = NetWorkManagerAlamofire.self
private let JSONManagerHandle = JSONManagerNSJSONSerializer.self

//
enum Method: String
{
    case OPTIONS = "OPTIONS"
    case GET = "GET"
    case HEAD = "HEAD"
    case POST = "POST"
    case PUT = "PUT"
    case PATCH = "PATCH"
    case DELETE = "DELETE"
    case TRACE = "TRACE"
    case CONNECT = "CONNECT"
}

let ERRORDOMINNETWORKMANAGER = "me.hp.netWorkManager"


public class NetWorkManager
{
    // TODO: 配置 NetWorkManagerHandle JSONManagerHandle 的方法
    
    public class func requestGET(#bussinessUrl: String, data: NetWorkParameterData, donefunc: (Result<NetWorkResultModel>) -> ()) -> NetWorkRequest
    {
        return NetWorkManagerHandle.request(bussinessUrl: bussinessUrl, data: data, method: .GET, donefunc: donefunc)
    }
    
    public class func requestPOST(#bussinessUrl: String, data: NetWorkParameterData, donefunc: (Result<NetWorkResultModel>) -> ()) -> NetWorkRequest
    {
        return NetWorkManagerHandle.request(bussinessUrl: bussinessUrl, data: data, method: .POST, donefunc: donefunc)
    }
    
    public class func cancelRequest(request: NetWorkRequest)
    {
        NetWorkManagerHandle.cancelRequest(request)
    }
    
    class func netWorkDataBack(#result: Result<NSData>, donefunc: (Result<NetWorkResultModel>) -> ()) -> Void
    {
        donefunc(result.handleTrackError(JSONManagerHandle.dataToDictionary).handleTrackError(dictionaryToModel))
    }
    
    class func dictionaryToModel(dict: Dictionary<String, AnyObject>) -> Result<NetWorkResultModel>
    {
        return .Error(NSError(domain: ERRORDOMINJSONMANAGER, code: -3, reason: ""))
    }
}

public class NetWorkRequest
{
    let cancelHandle: AnyObject?
    init(cancelHandle: AnyObject?)
    {
        self.cancelHandle = cancelHandle
    }
}

protocol NetWorkManagerProtocol
{
    static func request(#bussinessUrl: String, data: NetWorkParameterData, method: Method, donefunc: (Result<NetWorkResultModel>) -> ()) -> NetWorkRequest
    static func cancelRequest(request: NetWorkRequest)
}
